/* global window */
import pathToRegexp from 'path-to-regexp'
import modelExtend from 'dva-model-extend'
import { message } from 'antd'
import queryString from 'query-string'
import { list, create, update, remove, query, detailAdd } from '../../services/adminCompanyAccountDetail.js'// 细节的服务需要后面自己写好
import { pageModel } from '../common'
import * as companyChargeType from 'services/companyChargeType.js'
import * as basicData from 'services/basicData.js'
import * as AdminCompanyAccount from 'services/adminCompanyAccount'


export default modelExtend(pageModel, {
  namespace: 'adminCompanyAccountDetail',

  state: {
    companyChargeTypeData: [],
    currenciesTypeData: [],
    name: '',
    accountId: '',
    accountInfo: {},
    currentItem: {},
  },

  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen((pathname) => {
        const match = pathToRegexp('/adminCompanyAccount/detail/:id/:name').exec(pathname.pathname)
        if (match) {
          // const id = match[1].slice(0, match[1].length)// 根据id来查找
          const name = match[2]
          // const accountId = parseInt(id)
          const accountId = match[1]
          let payload
          if (location.search === '') {
            payload = { page: 1, accountId }
          } else {
            payload = queryString.parse(location.search)
          }
          dispatch({ type: 'list', payload: { ...payload } })
          dispatch({ type: 'iniBasicData',
            payload: {
              name,
              accountId,
            } })
        }
      })
    },
  },

  effects: {
    * list ({ payload = {} }, { call, put }) {
      const data = yield call(list, payload)
      const listData = data.data.data
      if (data.success) {
        yield put({
          type: 'updateState',
          payload: {
            list: listData,
            pagination: {
              current: Number(payload.page) || 1,
              pageSize: Number(payload.pageSize) || 10,
              total: data.data.pages,
            },
          },
        })
      }
    },
    * create ({ payload }, { call, put, select }) {
      const { accountId } = yield select(_ => _.adminCompanyAccountDetail)
      const data = yield call(detailAdd, payload)
      yield put({
        type: 'list',
        payload: {
          page: 1,
          pageSize: 10,
          accountId,
        },
      })
    },
    * update ({ payload }, { call, put,select }) {
      const { accountId } = yield select(_ => _.adminCompanyAccountDetail)
      const data = yield call(update, payload)
      if (data.success) {
        message.success('update success')
        yield put({ type: 'hideModal' })
        yield put({
          type: 'list',
          payload: {
            page: 1,
            pageSize: 10,
            accountId,
          },
        })
      } else {
        message.error('update failed')
        throw data
      }
    },

    * showModal ({ payload }, { call, put }) {
      yield put({
        type: 'updateModalState',
        payload: { ...payload },
      })
    },
    * delete ({ payload }, { call, put, select }) {
      const { accountId } = yield select(_ => _.adminCompanyAccountDetail)
      const data = yield call(remove, { ...payload })
      const { selectedRowKeys } = yield select(_ => _.adminCompanyAccountDetail)
      if (data.success) {
        message.success('delete success')
        yield put({ type: 'updateState', payload: { selectedRowKeys: selectedRowKeys.filter(_ => _ !== payload) } })
        yield put({
          type: 'list',
          payload: {
            page: 1,
            pageSize: 10,
            accountId,
          },
        })
      } else {
        message.error('delete failed')
        throw data
      }
    },

    * iniBasicData ({ payload }, { call, put }) {
      const name = payload.name
      const accountId = payload.accountId
      const companyChargeTypeRes = yield call(companyChargeType.list)
      const currenciesData = yield call(basicData.listall, { type: 'Currencies' })
      const adminCompanyAccountData = yield call(AdminCompanyAccount.query, { id: accountId })
      if (companyChargeTypeRes.success) {
        yield put({
          type: 'updateState',
          payload: {
            companyChargeTypeData: companyChargeTypeRes.data.data,
          },
        })
      }
      if (currenciesData.success) {
        yield put({
          type: 'updateState',
          payload: {
            currenciesTypeData: currenciesData.data,
          },
        })
      }
      yield put({
        type: 'updateState',
        payload: {
          name,
          accountId,
          accountInfo: adminCompanyAccountData.data,
        },
      })
    },
  },

  reducers: {
    updateCreateModalState (state, { payload }) {
      return { ...state, ...payload, createModalVisible: true }
    },
    hideCreateModal (state) {
      return { ...state, createModalVisible: false }
    },
  },
})
